package com.wt.struggle.goodbye.main.fragment

import android.Manifest
import android.app.Activity
import android.content.Intent
import android.content.pm.PackageManager
import android.net.Uri
import android.os.Build
import android.os.Bundle
import android.support.v4.app.ActivityCompat
import android.support.v4.content.FileProvider
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import com.wt.struggle.goodbye.R
import com.wt.struggle.goodbye.base.ProV4Fragment
import com.wt.struggle.goodbye.info.NetInfo
import com.wt.struggle.goodbye.main.activity.SendTieActivity
import com.xin.lv.yang.utils.utils.DataUtil
import com.xin.lv.yang.utils.utils.FileUtils
import com.xin.lv.yang.utils.utils.ImageUtil
import kotlinx.android.synthetic.main.camera_layout.*
import org.devio.takephoto.app.TakePhoto
import org.devio.takephoto.model.CropOptions
import org.devio.takephoto.model.TResult
import java.io.File

class CameraFragment : ProV4Fragment() {

    override fun handler(what: Int, arg1: Int, netInfo: NetInfo) {
    }

    override fun createView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View {
        return inflater.inflate(R.layout.camera_layout, container, false)
    }

    var takeP: TakePhoto? = null

    override fun onActivityCreated(savedInstanceState: Bundle?) {
        super.onActivityCreated(savedInstanceState)
        takeP = takePhoto

        imageCamera.setOnClickListener {
            val code1 = ActivityCompat.checkSelfPermission(activity!!, Manifest.permission.CAMERA)
            val code2 = ActivityCompat.checkSelfPermission(activity!!, Manifest.permission.READ_EXTERNAL_STORAGE)
            val code3 = ActivityCompat.checkSelfPermission(activity!!, Manifest.permission.WRITE_EXTERNAL_STORAGE)

            if (code1 == PackageManager.PERMISSION_GRANTED &&
                code2 == PackageManager.PERMISSION_GRANTED &&
                code3 == PackageManager.PERMISSION_GRANTED
            ) {

                takeP!!.onPickFromCaptureWithCrop(buildUri(), CropOptions.Builder().create())

            } else {
                ActivityCompat.requestPermissions(
                    activity!!, arrayOf(
                        Manifest.permission.CAMERA,
                        Manifest.permission.READ_EXTERNAL_STORAGE,
                        Manifest.permission.WRITE_EXTERNAL_STORAGE
                    ), 3456
                )
            }

        }
    }

    override fun onRequestPermissionsResult(requestCode: Int, permissions: Array<out String>, grantResults: IntArray) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults)
        if (requestCode == 3456) {
            if (grantResults[0] == PackageManager.PERMISSION_GRANTED &&
                grantResults[1] == PackageManager.PERMISSION_GRANTED &&
                grantResults[2] == PackageManager.PERMISSION_GRANTED
            ) {

                takeP!!.onPickFromCaptureWithCrop(buildUri(), CropOptions.Builder().create())
            }
        }
    }

    override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
        super.onActivityResult(requestCode, resultCode, data)
        if (requestCode == 1234 && resultCode == Activity.RESULT_OK) {

            val file = FileUtils.getImageFile()
            if (file.exists()) {
                ImageUtil.getInstance().loadImageNoTransformation(activity!!, imageCamera, 0, file.absolutePath)
                (activity as SendTieActivity).saveFile(file)
            }
        }
    }


    override fun takeSuccess(result: TResult?) {
        super.takeSuccess(result)

        log("执行到此信息--------")

        val arrInfo = result!!.image

        if (arrInfo != null) {

            val file = File(arrInfo.originalPath)

            log("result------"+file.exists())

            if (file.exists()) {
                ImageUtil.getInstance().loadImageNoTransformation(activity!!, imageCamera, 0, file.absolutePath)
                (activity as SendTieActivity).saveFile(file)
            }
        }

    }
}